package fr.cdj.thebox.shared;

import java.io.Serializable;

public class FileMetaData implements Serializable, Comparable<FileMetaData> {

	private static final long serialVersionUID = 9114245671581977973L;

	private static CompareFileType compareBy = CompareFileType.NAME;
	private static boolean ascending = true;

	public static void setCompareBy(CompareFileType type) {
		if (compareBy.equals(type)) {
			ascending = !ascending;
		} else {
			ascending = true;
			compareBy = type;
		}
	}

	public enum CompareFileType {
		NAME("Name"),
		LAST_MODIFIED("Last modified"),
		LENGTH("Size");

		private String name;

		private CompareFileType(String name) {
			this.name = name;
		}

		public String toString() {
			return name;
		}

	}

	private boolean directory;
	private boolean hidden;

	private Long lastModified;
	private Long length;

	private String name;
	private String path;

	public FileMetaData() {
	}

	public boolean isDirectory() {
		return directory;
	}

	public void setDirectory(boolean directory) {
		this.directory = directory;
	}

	public boolean isHidden() {
		return hidden;
	}

	public void setHidden(boolean hidden) {
		this.hidden = hidden;
	}

	public Long getLastModified() {
		return lastModified;
	}

	public void setLastModified(Long lastModified) {
		this.lastModified = lastModified;
	}

	public Long getLength() {
		return length;
	}

	public void setLength(Long length) {
		this.length = length;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	@Override
	public int compareTo(FileMetaData o) {
		int result = 0;
		switch (compareBy) {
		case LAST_MODIFIED:
			result = this.lastModified.compareTo(o.getLastModified());
			break;
		case LENGTH:
			result = this.length == null ? (o.getLength() == null ? this.name.toLowerCase().compareTo(o.getName().toLowerCase()) : 1) : (o.getLength() == null ? 1 : this.length.compareTo(o.getLength()));
			break;
		case NAME:
			result = this.name.toLowerCase().compareTo(o.getName().toLowerCase());
			break;
		default:
			break;
		}
		return ascending ? result : -result;
	}

}
